package com.haochang.chunkvod.view.vod.activity;

import com.haochang.base.BaseActivity;
import com.haochang.base.BaseEditText;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.common.intent.IntentKey;
import com.haochang.chunkvod.app.utils.HelperUtils;
import com.haochang.chunkvod.app.utils.SoftKeyboardUtils;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.model.hostess.HostessData;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;

/**
 *     @author Yangjie
 *     className OneKeyOpenRoomGrantDialogActivity
 *     created at  2016/9/1  19:33
 *     权限验证框 
 */
public class AuthorizationDialogActivity extends BaseActivity
{
	private BaseEditText dialog_grant_name, dialog_grant_pwd;
	private String interfaceName;
	private String event;

	@Override
	public void receiveParam()
	{
		interfaceName = getIntent().getStringExtra(IntentKey.INTERFACE_NAME);
		event = getIntent().getStringExtra(IntentKey.TOKEN_EVENT);
		if (TextUtils.isEmpty(interfaceName))
		{
			ToastUtils.showText(AuthorizationDialogActivity.this, "缺少接口名");
			finish();
		}
	}

	@Override
	public void initData()
	{

	}

	@Override
	public void initViews()
	{
		setContentView(R.layout.common_dialog_grant_toast);
		Button btn_sure = (Button) findViewById(R.id.btn_sure);
		Button btn_cancel = (Button) findViewById(R.id.btn_cancel);
		dialog_grant_name = (BaseEditText) findViewById(R.id.dialog_grant_name);
		dialog_grant_pwd = (BaseEditText) findViewById(R.id.dialog_grant_pwd);
		btn_sure.setOnClickListener(onBaseClickListener);
		btn_cancel.setOnClickListener(onBaseClickListener);
		String account = HelperUtils.getHelperInstance().getSValue("staff_number", "");
		if (!TextUtils.isEmpty(account))
		{
			dialog_grant_name.setText(account);
			dialog_grant_pwd.requestFocus();
		}
	}

	private OnBaseClickListener onBaseClickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			switch (v.getId())
			{
			case R.id.btn_sure:
				String name = dialog_grant_name.getText().toString().trim();
				final String pwd = dialog_grant_pwd.getText().toString().trim();
				if (TextUtils.isEmpty(name))
				{
					ToastUtils.showText(AuthorizationDialogActivity.this, "用户名不能为空");
				} else if (TextUtils.isEmpty(pwd))
				{
					ToastUtils.showText(AuthorizationDialogActivity.this, "密码不能为空");
				} else
				{
					SoftKeyboardUtils.closeSoftKeyBoard(AuthorizationDialogActivity.this, dialog_grant_name);
					new HostessData(AuthorizationDialogActivity.this).requestAuthorization(interfaceName, name, pwd, event,
							new HostessData.ResultResponseString()
							{
								@Override
								public void onRequestSuccess(String token)
								{
									if (!TextUtils.isEmpty(token))
									{
										setResult(RESULT_OK, getIntent().putExtra(IntentKey.GRANT_DIALOG_TOKEN, token));
										SoftKeyboardUtils.closeSoftKeyBoard(AuthorizationDialogActivity.this);
										finish();
									}
								}

								@Override
								public void onFail(int errno, String errMsg)
								{
									ToastUtils.showText(AuthorizationDialogActivity.this, TextUtils.isEmpty(errMsg) ? "权限验证失败，请稍后再试" : errMsg);
								}
							});
				}
				break;
			case R.id.btn_cancel:
				finish();
				break;
			}
		}
	};

}
